def collage(picture):
 h=getHeight(picture)
 w=getWidth(picture)
 newPic=makePicture(pickAFile())
 xOff=requestInteger("X Offset")
 yOff=requestInteger("Y Offset")
 W=getWidth(newPic)
 H=getHeight(newPic)
 for x in range(1,W):
  for y in range(1,H):
   if(x+xOff<w and y+yOff<h):
    pixel1=getPixel(newPic,x,y)
    clr=getColor(pixel1)
    Pixel2=getPixel(picture,x+xOff,y+yOff)
    setColor(Pixel2,clr)

def mirror(pic):
 pxl=getPixels(pic)
 w=getWidth(pic)
 h=getHeight(pic)
 for x in range(1,w):
  for y in range(1,h/2):
   clr=getColor(getPixel(pic,x,y))
   newPxl=getPixel(pic,x,h-y)
   setColor(newPxl,clr)


def chromakey(bg):
 bgH=getHeight(bg)
 bgW=getWidth(bg)

 greenpic=makePicture(pickAFile())


 for p in getPixels(greenpic):
  r=getRed(p)
  g=getGreen(p)
  b=getBlue(p)

  if (r+b>g):
   x=getX(p)
   y=getY(p)
   if (y<bgH)and(x<bgW):
    nuColor=getColor(p)
    bgPixel=getPixel(bg,x,y)
    setColor(bgPixel,nuColor)

   

def invert(picture):
 collection=getPixels(picture)
 for pixel in collection:
  r=getRed(pixel)
  g=getGreen(pixel)
  b=getBlue(pixel)
  r=255-r
  g=255-g
  b=255-b
  newColor=makeColor(r,g,b)
  setColor(pixel,newColor)






def grayscale(picture):
 collection=getPixels(picture)
 for pixel in collection:
  r=getRed(pixel)
  g=getGreen(pixel)
  b=getBlue(pixel)
  grey=(r+g+b)/3
  newColor=makeColor(grey,grey,grey)
  setColor(pixel,newColor)



bob=pickAFile()
pic=makePicture(bob)
show(pic)



choice=requestInteger("Choose a Filter\n0=grayscale 1=invert 2=chromakey 3=mirror 4=collage")
if(choice==0):
 grayscale(pic)
if(choice==1):
 invert(pic)
if(choice==2):
 chromakey(pic)
if(choice==3):
 mirror(pic)
if(choice==4):
 collage(pic)






#repaint the pic
repaint(pic)

#save as begins
fileName=requestString("Save As")
if fileName!=(''):
 fileName=pickAFolder()+'\\'+fileName+".jpg"
 printNow(fileName)
 writePictureTo(pic,fileName)
#save as ends